<?php

namespace App\Models\Traits;

use App\Models\Book;
use App\Models\OrderItem;
use App\Models\User;
use App\Models\UserCart;
use App\Models\UserLikeCategory;
use DB;
use Cache;

trait UserLikeCategoryHelper
{
    protected $cache_key = 'user_like_category';
    protected $book_total_read_num = 20;  //设定图书的借阅人数
    protected $cache_expire_in_minutes = 1440;  //24*60

    public function setAllUserLikeCategories()
    {
        $users = User::query()->where('user_type',1)->get();
        //$users = User::all();
        if(!empty($users))
        {
            foreach ($users as $v)
            {
                /*if($v->id != 2558)
                {
                    continue;
                }*/
                $result = $this->setUserLikeCategory($v->id);
                Cache::put($this->cache_key.':user_id_'.$v->id, $result, $this->cache_expire_in_minutes);
            }
        }
    }

    //获取单个用户记录
    public function getUserLikeCategory($user_id)
    {
        $cache_key = $this->cache_key.':user_id_'.$user_id;

        //Cache::forget($cache_key);
        //die;
        /*$result = $this->setUserLikeCategory($user_id);
        Cache::put($cache_key, $result, $this->cache_expire_in_minutes);
        die;*/
        return Cache::remember($cache_key, $this->cache_expire_in_minutes, function() use ($user_id){
            return $this->setUserLikeCategory($user_id);
        });
    }

    private function setUserLikeCategory($user_id)
    {
        $result = [];

        $total_score = UserLikeCategory::query()->where('user_id',$user_id)->sum('score');
        if($total_score > 0)
        {
            $list = UserLikeCategory::query()->where('user_id',$user_id)->get();
            if(!empty($list))
            {
                foreach ($list as $vv)
                {
                    //$order_book_ids_arr = [];
                    $order_book_ids_arr = OrderItem::query()->where('user_id',$user_id)->pluck('book_id')->toArray();
                    $cart_book_ids_arr = UserCart::query()->where('user_id',$user_id)->pluck('book_id')->toArray();
                    //print_r($order_book_ids_arr);
                    $not_book_ids_arr = array_unique(array_merge($order_book_ids_arr,$cart_book_ids_arr));

                    $book_num = ceil(($vv->score / $total_score) * 150);
                    //echo $book_num;
                    $book_ids_arr = Book::query()->where('on_sale',1)->where('deleted',0)->where('total_read_num', '>=', $this->book_total_read_num)->where('category_id', $vv->category_id)->where('store', '>', 0)->where('review_count','>=',1)->whereNotIn('id',$not_book_ids_arr)->limit($book_num)->pluck('id')->toArray();

                    //var_dump($book_ids_arr);
                    if(!empty($book_ids_arr))
                    {
                        $result[] = [
                            'category_id' => $vv->category_id,
                            'score' => $vv->score,
                            'book_ids_arr' => $book_ids_arr
                        ];
                    }
                    else
                    {
                        $result[] = [
                            'category_id' => $vv->category_id,
                            'score' => $vv->score,
                            'book_ids_arr' => [1808,1824,1840,1846,1869,14689,14699,14726,16574,10,4115,16,19355,19356,19357,8753,8754,8757,8760,8768,8775,8385,8789,8849,8853,8916,6,18,16597,16677,16700,16702,16436,11,9765,9766,9774,9776,9844,9846,9868,9887,9990,9994,10021,3277,10052,14144,14145,14153,14182,14155,1090,1101,19294,19342,19343,1923,19286,19287,19309,19311,19314,19316,19319,19323,19329,19338,12020,12022,11996,11976,3054,3168,7014,1085,1123,1160,11676,15645,15648,15650,15721,11925,11927,11944,11948,11961,11965,11972,11980,12015,12023,12022,9093,9106,7302,7301,7300,4269,4570,8293,8298,8303,8297,8320,8325,8335,8356,2434,3224,3225,3240,3277,3406,3411,3462,3498]
                        ];
                    }
                }
            }
        }
        else
        {
            $result[] = [
                'category_id' => 0,
                'score' => 0,
                'book_ids_arr' => [1808,1824,1840,1846,1869,14689,14699,14726,16574,10,4115,16,19355,19356,19357,8753,8754,8757,8760,8768,8775,8385,8789,8849,8853,8916,6,18,16597,16677,16700,16702,16436,11,9765,9766,9774,9776,9844,9846,9868,9887,9990,9994,10021,3277,10052,14144,14145,14153,14182,14155,1090,1101,19294,19342,19343,1923,19286,19287,19309,19311,19314,19316,19319,19323,19329,19338,12020,12022,11996,11976,3054,3168,7014,1085,1123,1160,11676,15645,15648,15650,15721,11925,11927,11944,11948,11961,11965,11972,11980,12015,12023,12022,9093,9106,7302,7301,7300,4269,4570,8293,8298,8303,8297,8320,8325,8335,8356,2434,3224,3225,3240,3277,3406,3411,3462,3498]
            ];
        }

        return $result;
    }
}